package com.magic.zhixin.controller;

import com.magic.zhixin.entity.UserInfo;
import com.magic.zhixin.model.FrontThirdSessionRequestEntity;
import com.magic.zhixin.service.SnsAccessService;
import com.magic.zhixin.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created by liuhangjun on 2018/3/6.
 */
@Api(tags = "用户登录会话接口")
@RestController
@RequestMapping(value = "/api/session")
public class SessionController extends BaseController {


	@Resource
	private UserInfoService userInfoService;

	@Autowired
	private SnsAccessService snsAccessService;


	@ApiOperation(value = "前端(h5和小程序)第三方登录, 返回用户信息")
	@PostMapping("/frontThird")
	public UserInfo frontThirdLogin(@RequestBody @Validated FrontThirdSessionRequestEntity frontThirdSessionRequestEntity) throws IOException {


		UserInfo userInfo = snsAccessService.getUserInfoFromThirdParty( frontThirdSessionRequestEntity.getCode(), frontThirdSessionRequestEntity.getMinaVerifyMap());

		UserInfo retUserInfo = userInfoService.thirdLogin(userInfo);

		super.addUserToSession(retUserInfo);

		return retUserInfo;
	}

}
